package com.koron.plan.web.patrollineplan.service.impl;


import com.koron.plan.web.patrollineplan.bean.po.PlanLinePatrolCheck;
import com.koron.plan.web.patrollineplan.mapper.PlanLinePatrolCheckMapper;
import com.koron.plan.web.patrollineplan.service.PlanLinePatrolCheckService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 线路巡视检查项(PlanLinePatrolCheck)表服务实现类
 *
 * @author jzp
 * @since 2022-11-24 14:24:19
 */
@Service
public class PlanLinePatrolCheckServiceImpl implements PlanLinePatrolCheckService {

    /**
     * 通过ID查询单条数据
     *
     */
    @Override
    @TaskAnnotation("getById")
    public PlanLinePatrolCheck getById(SessionFactory factory, String id) {
        PlanLinePatrolCheckMapper mapper = factory.getMapper(PlanLinePatrolCheckMapper.class);
        return mapper.selectById(id);
    }

    /**
     * 查询所有数据
     *
     */
    @Override
    @TaskAnnotation("list")
    public List<PlanLinePatrolCheck> list(SessionFactory factory, PlanLinePatrolCheck planLinePatrolCheck) {
        PlanLinePatrolCheckMapper mapper = factory.getMapper(PlanLinePatrolCheckMapper.class);
        return mapper.selectList(planLinePatrolCheck);
    }

    @Override
    public List<PlanLinePatrolCheck> listByPlanId(SessionFactory factory, String planId) {
        PlanLinePatrolCheck planLinePatrolCheck = new PlanLinePatrolCheck();
        planLinePatrolCheck.setPlanId(planId);
        return list(factory, planLinePatrolCheck);
    }

    /**
     * 新增数据
     *
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, PlanLinePatrolCheck planLinePatrolCheck) {
        PlanLinePatrolCheckMapper mapper = factory.getMapper(PlanLinePatrolCheckMapper.class);
        return mapper.insertOrUpdate(planLinePatrolCheck) > 0;
    }

    @Override
    public boolean saveBatch(SessionFactory factory, List<PlanLinePatrolCheck> checkList) {
        if (CollectionUtils.isEmpty(checkList)) {
            return false;
        }
        PlanLinePatrolCheckMapper mapper = factory.getMapper(PlanLinePatrolCheckMapper.class);
        return mapper.insertOrUpdateBatch(checkList) > 0;
    }

    /**
     * 修改数据
     *
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, PlanLinePatrolCheck planLinePatrolCheck) {
        PlanLinePatrolCheckMapper mapper = factory.getMapper(PlanLinePatrolCheckMapper.class);
        return mapper.update(planLinePatrolCheck) > 0;
    }

    /**
     * 通过主键删除数据
     *
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        PlanLinePatrolCheckMapper mapper = factory.getMapper(PlanLinePatrolCheckMapper.class);
        return mapper.hidden(id) > 0;
    }

    @Override
    public boolean deleteBatchIds(SessionFactory factory, List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        PlanLinePatrolCheckMapper mapper = factory.getMapper(PlanLinePatrolCheckMapper.class);
        return mapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public boolean removeByPlanId(SessionFactory factory, String planId) {
        if (StringUtils.isEmpty(planId)) {
            return false;
        }
        List<PlanLinePatrolCheck> honorList = listByPlanId(factory, planId);
        return deleteBatchIds(factory, honorList.stream().map(PlanLinePatrolCheck::getId).collect(Collectors.toList()));
    }

}
